package com.dk.controller;

import com.dk.VO.TbCarsVO;
import com.dk.pojo.Tbcar;
import com.dk.result.Result;
import com.dk.service.Carservice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cars")
@RefreshScope      //自动刷新配置, nacos的
@Api
@ApiOperation("车辆管理接口")
public class CarsController {
    @Autowired
    private Carservice carservice;
    //查询全部车辆信息
    @GetMapping("getAll")
    @ApiOperation(value = "查询车辆列表")
    public Result getAll(){
        try {
            List<Tbcar> carlixt = carservice.getAll();
            return Result.OK().setData(carlixt);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //通过id查询车辆信息
    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id查询车辆")
    @ApiImplicitParam(name="id",value = "车辆主键",required = true, paramType = "path")
    public Result getById(@PathVariable("id") Long id){
        try {
           TbCarsVO vo =  carservice.getById(id);
           return Result.OK().setData(vo);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //增加车辆信息
    @PostMapping("addcar")
    @ApiOperation(value = "增加车辆数据")
    @ApiImplicitParam(name="car",value = "车辆实体数据",required = true, paramType = "body")
    public Result addcar(@RequestBody TbCarsVO car){
        try {
            carservice.insert(car);
            return Result.OK();
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //删除车辆信息
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据车辆id删除车辆")
    @ApiImplicitParam(name="id",value = "车辆主键",required = true, paramType = "path")
    public Result deleteById(@PathVariable("id") Long id){
        try {
            carservice.deleteById(id);
            return Result.OK();
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
    //修改车辆信息
    @PutMapping("updatecar")
    @ApiOperation(value = "修改车辆数据")
    @ApiImplicitParam(name="car",value = "车辆实体数据",required = true, paramType = "body")
    public Result updatecar(@RequestBody TbCarsVO car){
        try {
            carservice.update(car);
            return Result.OK();
        }catch (Exception e) {
            e.printStackTrace();
            return Result.ER();
        }
    }
}
